import message from '@/message'
import {PageModule} from '@/PageModule'

//资源加载器
class Loader{
    constructor() {
        this.staticModules={};
    }

    loadResource(opts) {
        //加载静态资源
        const {appId,pagePath}=opts;
        const viewResourcePath=`/mini_resource/${appId}/view.js`
        const styleResourcePath=`/mini_resource/${appId}/style.css`
        const script =document.createElement('script');
        const link=document.createElement('link');

        modRequire(pagePath);

        Promise.all([
            this.loadStyleFile(link,styleResourcePath),
            this.loadScriptFile(script,viewResourcePath),
        ]).then(()=>{
            message.send({
                type:'uiResourceLoaded',
                body:{}
            })
        });
    }

    loadStyleFile(link,path) {
        return new Promise((resolve,reject)=>{
            link.rel='styleSheet';
            link.href=path;
            link.onload=()=>{
                resolve();
            }
            link.onerror=()=>{
                reject();
            }
            document.body.appendChild(link);
        })
    }
    
    loadScriptFile(script,path) {
        return new Promise((resolve,reject)=>{
            script.href=path;
            link.onload=()=>{
                resolve();
            }
            link.onerror=()=>{
                reject();
            }
            document.body.appendChild(script);
        })
    }
    
    createPageModule(moduleInfo){
        const pageModule=new PageModule(moduleInfo);
        const {path}=moduleInfo;
        this.staticModules[path]=pageModule;
    }
    
    getModuleByPath(path){
        return this.staticModules[path];
    }
    setInitialData(initialData){
        for(let [path,data] of Object.entries(initialData)){
            const staticModule=this.staticModules[path];
            if(!staticModule){
                continue;
            }
            staticModule.setInitialData(data);
        }
    }
}
export default new Loader();